// Trade-Policy Dynamics: Evidence from 60 Years of U.S.-China Trade
// Alessandria,  Khan, Khederlarian, Ruhl, and Steinberg

//input: Feenstra/imp74.dta ... imp88.dta

// output: dataset_tsusa.dta

// This code constructs a product-level data set from the TSUSA data, 1974--88. 

set varabbrev off 
clear all 

global dir_int "../20 Intermediate files/"
global dir_raw "../01 Raw data/"

////////////////////////////////////////////////////////////////////////////////
// Generate TSUSA dataset 

// 1974-1988 from Feenstra
{
	use "${dir_raw}Feenstra/imp74/imp74.dta", clear
	forvalues x = 75/88 {
		append using "${dir_raw}Feenstra/imp`x'/imp`x'.dta"
	}
	rename yr year
	rename duty duties
	gen country = name
	gen tar_unwgt = duties/cusvalue
	replace tar_unwgt = 0 if tar_unwgt<0
	bysort sitc2 year country: egen count_jst = nvals(tsusa)
	collapse (mean) tar_unwgt (sum) quantity cusvalue cifvalue duties (first) sitc2 count_jst units , by(tsusa year country)
	replace year = year+1900
	*save "Stata Files\imports_tsusa_74-88.dta", replace
}


// Clean up, create variables, etc. 
{
	drop tar_unwgt count_jst units
	rename quantity q_jst
	rename cusvalue v_jst
	rename cifvalue vcif_jst
	rename duties duties_jst

	gen cty=lower(country)
	drop country
	encode cty, gen(ctyn)
	egen id = group(cty tsusa)
	encode tsusa, gen(tsusan)

	drop if cty=="unknown"

	*** Dummies
	gen ntr_cty =  cty!="ussr" & cty!="china" & cty!="romania" & cty!="hungary" & cty!="czecho" & cty!="cuba" & cty!="vietnam" & cty!="cambod" & cty!="albania" & cty!="bulgaria" & cty!="korea_n" ///
				& cty!="germen_e"
	*** Note: Yugoslavia and poland were exempted from NNTR treatment after 1962
	gen fta= (cty=="israel" & year>1984) | (cty=="jordon" & year>2000) | (cty=="austral" & year>2004) | (cty=="singapr" & year>2003) ///
	| (cty=="chile" & year>2003) | (cty=="bahrain" & year>2005) | (cty=="morocco" & year>2005) | (cty=="oman" & year>2005) | (cty=="peru" & year>2007) ///
	| (cty=="cos_rica" & year>2008) | (cty=="salvadr" & year>2008) | (cty=="guatmala" & year>2008) | (cty=="hondura" & year>2008) ///
	| (cty=="nicaraga" & year>2008) | (cty=="dom_rep" & year>2008) | (cty=="korea_s" & year>2009) ///
	| (cty=="colombia" & year>2011) | (cty=="panama" & year>2011) | (cty=="japan" & year>2019) | (cty=="canada" & year>1987) | (cty=="mexico" & year>1993)
	gen chn = cty=="china"
	gen ussr = cty=="ussr"
	gen czech = cty=="czecho"
	gen roma = cty=="romania"
	gen hunga = cty=="hungary"
	gen viet = cty=="vietnam"
	gen hk = cty=="hongkong"
	gen twn = cty=="taiwan"

	*** Balanced sample
	bysort cty tsusa: egen tempmin = min(year)
	gen tempchn = tempmin<1980 & chn==1
	bysort tsusa: egen balanced_chn = max(tempchn)
	drop temp*

	*** Create some variables
	gen lv_jst = log(v_jst)
	gen tariff_jst = duties_jst/v_jst
	gen ltariff_jst = log(1+tariff_jst)
	gen shipping_jst = (vcif_jst-v_jst)/v_jst
	replace shipping_jst=0 if shipping_jst<0
	gen lshipping_jst = log(1+shipping_jst)
	bysort tsusa: egen lnntr_chn = mean(ltariff_jst/(year<1980 & chn==1))
	bysort tsusa: egen lnntr_allcom = mean(ltariff_jst/(year<1980 & ntr_cty==0))
}

sort cty tsusa year 
order cty* tsusa* id year 
compress
save "${dir_int}dataset_tsusa.dta", replace

